package com.polaris.live.ui.user

import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import com.polaris.live.adapter.ActCenterAdapter
import com.polaris.live.common.mvvm.base2.BaseVbActivity
import com.polaris.live.common.network.parseState
import com.polaris.live.common.util.ImmersionBarBuilder
import com.polaris.live.common.util.ToastGravity
import com.polaris.live.databinding.ActivityCenterActBinding
import com.polaris.live.resp.back_resp.ActCenterBaseVo
import com.polaris.live.utils.route.RouterUtils
import com.polaris.live.viewmodel.ActCenterViewModel

/**
 * 活动中心
 *
 * @author Created by 半仙 on 2024/7/29/029 14:59
 */
class ActCenterActivity : BaseVbActivity<ActCenterViewModel, ActivityCenterActBinding>() {

    private val mAdapter by lazy { ActCenterAdapter() }

    override fun ImmersionBarBuilder.buildImmersionBar() {
        setTitleBar(mBinding.barView)
        attachImmersionBar { transparentStatusBar() }
    }

    override fun initView(savedInstanceState: Bundle?) {
        mBinding.acRecycler.apply {
            adapter = mAdapter
            layoutManager = LinearLayoutManager(context)
        }
        mViewModel.getActCenter()
        initObserver()
    }

    private fun initObserver() {
        mViewModel.mActCenterLiveData.observe(this) { resultState ->
            parseState(resultState, {
                val data = buildList {
                    if (it.notStart.isNotEmpty()) {
                        add(NOTSTART)
                        addAll(it.notStart)
                    }
                    if (it.processing.isNotEmpty()) {
                        add(PROCESSING)
                        it.processing.forEach { it.processing = true }
                        addAll(it.processing)
                    }
                    if (it.ended.isNotEmpty()) {
                        add(ENDED)
                        addAll(it.ended)
                    }
                }
                mAdapter.setList(data)
            }, {
                ToastGravity.showGravity(it.errorMsg)
            })
        }
        mAdapter.setOnItemClickListener { _, _, position ->
            val item = mAdapter.getItemOrNull(position) ?: return@setOnItemClickListener
            if (item is ActCenterBaseVo && item.url?.isNotEmpty() == true) {
                RouterUtils.routerGoto(item.url)
            }
        }
    }

    companion object {
        const val ENDED = 1         //已结束
        const val PROCESSING = 2    //进行中
        const val NOTSTART = 3      //未开始
    }
}